var grpc = require('grpc');
var PROTO_PATH = 'E:\\work\\git\\grpc-client\\grpc\\src\\main\\proto\\service.proto';
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true
    });
var protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
var hello = protoDescriptor.com.xsnet.grpc;

/*
    创建RPC客户端
*/
function getGrpcClient() {
    //创建客户端
    let client = new hello.Hello('localhost:50010', grpc.credentials.createInsecure());
    return client;
}


function helloServer(request) {
    let client = getGrpcClient();

    client.sayHello(request, function (error, response) {
        console.log('result:', JSON.stringify(response));
    })
}

//测试
helloServer({
    name: 'jason'
});

exports.helloServer = helloServer;